package com.example.maili.woxiamg.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.maili.woxiamg.R;
import com.example.maili.woxiamg.api.APIS;
import com.example.maili.woxiamg.base.BaseActivity;
import com.example.maili.woxiamg.base.ConstantClass;
import com.example.maili.woxiamg.utils.AppUtils;
import com.example.maili.woxiamg.utils.OkHttp3Manager;

import org.json.JSONObject;

import okhttp3.Request;

public class LoginActivity extends BaseActivity {

    private EditText et_username, et_password;
    private TextView btn_login, tv_register, tv_forgetPasswordbtn;
    private ImageView img_qq, img_weibo, img_weixin;
    private String username, password;

    @Override
    public void initView() {
        setContentView(R.layout.activity_login);
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (TextView) findViewById(R.id.btn_login);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_forgetPasswordbtn = (TextView) findViewById(R.id.tv_forgetPasswordbtn);
        img_weibo = (ImageView) findViewById(R.id.img_weibo);
        img_qq = (ImageView) findViewById(R.id.img_qq);
        img_weixin = (ImageView) findViewById(R.id.img_weixin);

        img_qq.setOnClickListener(this);
        img_weibo.setOnClickListener(this);
        img_weixin.setOnClickListener(this);
        tv_forgetPasswordbtn.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void initdata() {

    }

    @Override
    protected void onClickListener(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.btn_login://登录
                userLongin();
                break;
            case R.id.img_qq://QQ登录
                break;
            case R.id.img_weibo://微博登录
                break;
            case R.id.img_weixin://微信登录
                break;
            case R.id.tv_forgetPasswordbtn://忘记密码
                intent=new Intent(context,ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_register://注册
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ConstantClass.SUCCESS:
                    break;
                case ConstantClass.FAIL:
                    break;
            }
        }
    };

    private void userLongin() {
        submit();
        OkHttp3Manager.getAsyn(APIS.LOGIN, new OkHttp3Manager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                if (response == null || TextUtils.isEmpty(response)) {
                    AppUtils.toast(context, "登录失败");
                    return;
                }
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response);
                    int sate = jsonObject.getInt("sate");
                    int userId = jsonObject.getInt("userId");
                    if (sate == -1) {
                        AppUtils.toast(context, "登录失败,帐号或密码错误!");
                    } else {
                        AppUtils.toast(context, "登录成功");
                        AppUtils.setSharedPreferences(context, userId + "", "int");
                        AppUtils.setSharedPreferences(context, username, "String");
                        AppUtils.setSharedPreferences(context, password, "String");
                        startActivity(new Intent(context, MainActivity.class));
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new OkHttp3Manager.Param("account", username), new OkHttp3Manager.Param("password", password));
    }

    private void submit() {
        username = et_username.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            AppUtils.toast(context, "用户名不能为空");
            return;
        }
        password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            AppUtils.toast(context, "密码不能为空");
            return;
        }
    }
}
